﻿using BLL.CodeManage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using Model.CodeManage;
using Enums;
using Enums.Sys;
using Common;
using Model.Organization;
using BLL.Organization;
namespace DAGG.Controllers.CodeManage
{
    public class GenCodeController : BaseController
    {
        GenCodeBLL bll = new GenCodeBLL();
        EnterpriseBLL bll0 = new EnterpriseBLL();

        //
        // GET: /Product/
        public ActionResult Index(string term = "", int page = 1)
        {

            IPagedList<GenCodeModel> modelInfoList = bll.GetList(term).ToPagedList(page, 10);

            ViewBag.MyPageList = modelInfoList;
            return View();
        }

        public ActionResult Edit(string id = "0")
        {
            string batch = string.Empty;
            GenCodeModel model = new GenCodeModel();


            if (id == "add")
            {//新增界面进入


                //自动生成生码批次信息
                GenCodeBatch batchModel = bll.GetMaxBatchByEnterId(1);

                if (batchModel == null)
                    batch = "01";
                else
                    batch = (PageValidate.ConvertToTrimInt(batchModel.Batch) + 1).ToString().PadLeft(2, '0');
                model.Batch = batch;
            }
            else//编辑界面进入
            {
                //获得id对应的生码任务信息 用于回显
                model = bll.GetModel(int.Parse(id));
            }
            //绑定公司下拉框
            List<EnterpriseModel> list = bll0.GetList("","");
            List<SelectListItem> selectListItem = new List<SelectListItem>();

            foreach (var item in list)
            {
                bool Istrue = false;
                if (item.ID == model.EnterpriseId) Istrue = true;
                selectListItem.Add(new SelectListItem { Text = item.EnterpriseName, Value = item.ID.ToString(), Selected = Istrue });
            }
            ViewData["selectList"] = selectListItem;
            return View(model);

        }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveData(GenCodeModel genCode)
        {
          
            if (genCode.ID == 0)
            {//新增
                //先创建生码批次表信息
                GenCodeBatch batch = new GenCodeBatch
                {
                    // EnterpriseId = 1,
                    EnterpriseId = PageValidate.ConvertToTrimInt(genCode.EnterpriseId),
                    Batch = genCode.Batch.PadLeft(2, '0'),
                    ViewFlag = EnumManagement.GetEnumValue(SysEnum.Enable),
                    Sequence = 99999,
                    CreateLoginID = base.GetUserInfo().ID,
                    CreateDate = DateTime.Now,
                    CreateLoginName = base.GetUserInfo().RealName,
                    ChangeDate = DateTime.Now,
                    ChangeLoginID = base.GetUserInfo().ID,
                    ChangeLoginName = base.GetUserInfo().RealName
                };

                GenCodeBatchBLL batchBll = new GenCodeBatchBLL();
                ResultEntity batchEntity = batchBll.AddPatch(batch);
                if (batchEntity != null && batchEntity.ResultState == Enums.EnumResultState.Success)
                {

                    //创建生码任务表信息
                    genCode.PatchId = PageValidate.ConvertToTrimInt(batchEntity.Id);
                    genCode.CreateDate = DateTime.Now;
                    genCode.CreateLoginID = base.GetUserInfo().ID;
                    genCode.CreateLoginName = base.GetUserInfo().RealName;
                    genCode.ChangeDate = DateTime.Now;
                    genCode.ChangeLoginID = base.GetUserInfo().ID;
                    genCode.ChangeLoginName = base.GetUserInfo().RealName;
                    genCode.Status = "0";//待生码状态
                    genCode.ViewDel = EnumManagement.GetEnumValue(SysEnum.NoDele);
                    //genCode.EnterpriseId = 1;//档案馆
 
                    //生成码的类型文本信息
                    string[] strValue = genCode.CodeType.TrimEnd(',').Split(',');
                    string CodeText = string.Empty; string codeTemp = string.Empty;
                    if (strValue != null && strValue.Length > 0)
                    {
                        foreach (string str in strValue)
                        {
                            codeTemp = string.Empty;
                            if (str == "1") codeTemp = "防伪码";
                            if (str == "2") codeTemp = "管理码";
                            if (str == "3") codeTemp = "积分码";

                            CodeText += codeTemp + ",";
                        }
                        CodeText = CodeText.TrimEnd(',');
                    }
                    genCode.CodeTypeText = CodeText;
                    if (genCode.ExpiryDate == "0")//无限期
                        genCode.DisableTime = PageValidate.ConvertToDateTime("3000-12-30");
                    else
                        genCode.DisableTime = DateTime.Now.AddYears(PageValidate.ConvertToTrimInt(genCode.ExpiryDate));
                        batchEntity = bll.Add(genCode);

                }

            }
            else//编辑
            {
                genCode.ChangeDate = DateTime.Now;
                genCode.ChangeLoginID = base.GetUserInfo().ID;
                genCode.ChangeLoginName = base.GetUserInfo().RealName;
                genCode.Status = "0";//待生码状态
                genCode.ViewDel = EnumManagement.GetEnumValue(SysEnum.NoDele);
                ResultEntity entity = bll.Update(genCode);

            }
            //bll.SaveData(genCode);
            return Redirect("/GenCode/Index");
        }

        public ActionResult BindBacth(int enterId)
        {

            string batch = string.Empty;
            //自动生成生码批次信息
            GenCodeBatch batchModel = bll.GetMaxBatchByEnterId(enterId);

            if (batchModel.Batch == null)
                batch = "01";
            else
                batch = (PageValidate.ConvertToTrimInt(batchModel.Batch) + 1).ToString().PadLeft(2, '0');

            return Json(batch);
        }
        public ActionResult DownCode(string term = "", int page = 1)
        {
            IPagedList<GenCodeModel> modelInfoList = bll.GetList(term).ToPagedList(page, 10);

            ViewBag.MyPageList = modelInfoList;
            return View();
        }
    }
}